package com.spring.cloud.practice.demo.springbootrabbitmq.consumer;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.spring.cloud.practice.demo.springbootrabbitmq.entity.Order;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * <b><code>TopicRecevier</code></b>
 * <p/>
 * Description
 * <p/>
 * <b>Creation Time:</b> 21-3-18 下午7:15.
 *
 * @author huanghuayan
 * @since spring-cloud-demo
 */
@Component
public class TopicRecevier {

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "order-queue", durable = "true"),
            exchange = @Exchange(value = "order-topic-exchange", durable = "true", type = "topic"),
            key = "order.#"
    ))
    @RabbitHandler
    public void onOrder(@Payload Order order, @Headers Map<String, Object> headers, Channel channel) throws Exception {
        System.out.println("--------------收到消息---------------");
        System.out.println("Recevier's orderId: " + order.getId());
        long deliveryTage = (long) headers.get(AmqpHeaders.DELIVERY_TAG);
        channel.basicAck(deliveryTage, false);
    }
}
